package com.atcc.demo.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadLocalDateFormat {
    private final ThreadLocal<SimpleDateFormat> THREAD_LOCAL;

    public ThreadLocalDateFormat(final String format) {
        this.THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() {
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat(format);
            }
        };
    }

    public String format(Date date) {
        return ((SimpleDateFormat) this.THREAD_LOCAL.get()).format(date);
    }

    public Date parse(String date) throws ParseException {
        return ((SimpleDateFormat) this.THREAD_LOCAL.get()).parse(date);
    }
}
